// pages/zdSetup/zdSetup.js
var app = getApp()
var header = {
  'content-type': 'application/x-www-form-urlencoded',
  'cookie': "openid=" + app.globalData.openid
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showLoading: true, // 等待动画
    fzList: [],
    keepSheetHidden: true,
    keepSheetItems: [
      { bindtap: 'Menu1', txt: '全开' },
      { bindtap: 'Menu2', txt: '全关' },
      { bindtap: 'Menu3', txt: '取消' },
    ],
    menu: '',
    chidx: 0,// 选中地区下标
    opType: '',// 1==开，0==关
    chmid:"", // 选中地区的mid 现在用的
    //opType: '',// 1==开，0==关
    cur_district_allbranch:{},
    all_district_allbranch:{},
    cur_district:"",
    branchdevNoliststr:""  //用来closeall 或者openall的devno逗号隔开的字符串
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //let name = app.globalData.login_name;
    this.getUserMeters()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 页面显示
    //this.checkLogin()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //this.setData({ fzList: [], keepSheetHidden: true, showLoading: true })
    //this.getUserMeters()
  },

  checkLogin: function () {
    var name = wx.getStorageSync('userName')
    if (name == '' || name == null) {
      wx.showModal({
        title: '提示',
        content: "用户未登录",
        confirmText: '去登录',
        showCancel: false,
        success: function () {
          wx.navigateTo({
            url: '../login/login',
          })
        }
      })
    }
  },
  actionSheetTap: function (e) {
    var idx = e.currentTarget.dataset.idx
    this.setData({
      chidx: idx, keepSheetHidden: !this.data.keepSheetHidden
    })
  },
  keepSheetChange: function () {
    this.setData({
      keepSheetHidden: !this.data.keepSheetHidden
    })
  },
  bindMenu1: function () {
    this.setData({
      menu: 1, opType: 1,
      keepSheetHidden: !this.data.keepSheetHidden
    })
  },
  bindMenu2: function () {
    this.setData({
      menu: 2, opType: 0,
      keepSheetHidden: !this.data.keepSheetHidden
    })
  },
  bindMenu3: function () {
    this.setData({
      menu: 3, opType: '',
      keepSheetHidden: !this.data.keepSheetHidden
    })
  },
  getUserMeters: function () {
    var that = this
    wx.request({
      url: app.globalData.url + 'loadTree',
      data:{
        userid:app.globalData.userid
      },
      method: 'POST',
      header: header,
      success: function (res) {
        //console.log(res, "res");
        let  data = JSON.parse(res.data.data)

        if (data.length != 0) {
          //这边数据需要重新组织 [{"attributes":{"mid":1,"type":0},"id":100000001,"text":"测试区域2","state":"closed","iconCls":"area_img"},{"attributes":{"mid":44,"type":0},"id":100000044,"text":"沛县","state":"closed","iconCls":"area_img"},{"attributes":{"mid":45,"type":0},"id":100000045,"text":"测试","state":"closed","iconCls":"area_img"},{"attributes":{"mid":46,"type":0},"id":100000046,"t...}]
         that.optionList(data)
        } else if(res.errMsg != "request:ok"){
          wx.showModal({
            title: '提示',
            content: res.errMsg,
            showCancel: false
          })
        }
      },
      complete: function () {
        wx.stopPullDownRefresh()
        that.setData({ showLoading: false })
      }
    })   
  },
  // 操作数据分组
  optionList: function (list) {
    var objs = []
    if(list.length > 0){      
      list.forEach(element => {
        let tmp_arr = {}
        tmp_arr.name = element.text
        tmp_arr.mid = element.attributes.mid
        tmp_arr.id = element.id
        objs.push(tmp_arr)        
      });
      //console.log(objs, "objs")
      this.setData({ fzList: objs })
    }    
   
  },
  // 跳转
  showSzByName: function (e) {
    //console.log(e, "e")
    var idx = e.currentTarget.dataset.idx    
    let mid = e.currentTarget.dataset.mid
    let district  = e.currentTarget.dataset.district
    let treeid = e.currentTarget.dataset.treeid
    //console.log(treeid, "treeid")
   
    wx.navigateTo({
      url: '../zdarea/zdarea?mid='+mid+'&district='+district+'&id='+treeid
    })
    // var idx = e.currentTarget.dataset.idx
    // var list = this.data.fzList
    // wx.setStorageSync("area" + idx, list[idx])// 缓存数据
    // wx.navigateTo({
    //   url: '../zdarea/zdarea?idx=' + idx,
    // })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})